package com.app.mp3player;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.app.mp3player.adapter.LRCListAdapter;
import com.app.mp3player.lrc.LrcProcessor;
import com.app.mp3player.model.Mp3Info;
import com.app.mp3player.service.PlayService;
import com.app.utils.AppConstant;

public class PlayActivity extends Activity {
	
	private static final String TAG = "PlayActivity";
	private Mp3Info mp3Info;
	
	private ImageView beginBtn;
	private ImageView pauseBtn;
	private ListView lrcListView;
	private TextView currentTextView;
	
	private IntentFilter intentFilter;
	private LRCListAdapter adapter;
	private LrcMessageBroadcastReceiver receiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.play);
		
		mp3Info = (Mp3Info)getIntent().getSerializableExtra("mp3Info");
		
		beginBtn = (ImageView)findViewById(R.id.play_btn);
		pauseBtn = (ImageView)findViewById(R.id.stop_btn);
		currentTextView = (TextView)findViewById(R.id.current_time_text);
		
		lrcListView = (ListView)findViewById(R.id.lrc_textList);
		
		beginBtn.setOnClickListener(new BeginBtnOnClickListener());
		pauseBtn.setOnClickListener(new PauseBtnOnClickListener());
		
		List<String> lrcList = prepareLrc(mp3Info.getLrcName());
		
		Log.i(TAG, lrcList.toString());
		adapter = new LRCListAdapter(this, lrcList);
		lrcListView.setAdapter(adapter);
	}
	
	public List<String> prepareLrc(String lrcName){
		List<String> lists = new ArrayList<String>();
		try{
			
			InputStream inputStream = new FileInputStream(Environment.getExternalStorageDirectory().getAbsoluteFile()
					+ File.separator + "mp3/" + mp3Info.getLrcName());
			LrcProcessor lrcProcessor = new LrcProcessor();
			List<Object> queues = lrcProcessor.process(inputStream);
			List<String> messages = (List<String>)queues.get(1);
			for (String msg : messages) {
				lists.add(msg);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return lists;
	}
	
	class BeginBtnOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(PlayActivity.this, PlayService.class);
			intent.putExtra("mp3Info", mp3Info);
			intent.putExtra("Msg", AppConstant.PLAY_ACTION.PLAY);
			beginBtn.setVisibility(View.GONE);
			pauseBtn.setVisibility(View.VISIBLE);
			startService(intent);
		}
		
	}
	
	class PauseBtnOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(PlayActivity.this, PlayService.class);
			intent.putExtra("mp3Info", mp3Info);
			intent.putExtra("Msg", AppConstant.PLAY_ACTION.PAUSE);
			beginBtn.setVisibility(View.VISIBLE);
			pauseBtn.setVisibility(View.GONE);
			startService(intent);
		}
		
	}
	
	
	public IntentFilter getIntentFilter(){
		if(intentFilter == null){
			intentFilter = new IntentFilter();
			intentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION);
		}
		return intentFilter;
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(receiver);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		receiver = new LrcMessageBroadcastReceiver();
		registerReceiver(receiver, getIntentFilter());
	}
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			int position = msg.what;
			/*LinearLayout item = (LinearLayout)lrcListView.getChildAt(position);
			TextView textView = (TextView)item.findViewById(R.id.lrc_text);
			textView.setTextColor(getResources().getColor(R.color.white));*/
			lrcListView.setPadding(0, -position*30, 0, 0);
			Bundle data = msg.getData();
			long currentTime = data.getLong("currentTime")/1000;
			String minu = Math.floor(currentTime / 60) > 10 ? (int)Math.floor(currentTime / 60)+"" : "0"+(int)Math.floor(currentTime / 60);
			String second = (currentTime % 60)>10?(currentTime % 60)+"":"0"+(currentTime % 60);
			currentTextView.setText(minu+":"+second);
			super.handleMessage(msg);
		}
		
	};
	
	class LrcMessageBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			int line = intent.getIntExtra("line",0);
			long currentTime = intent.getLongExtra("currentTime",0);
			Log.i(TAG, "LrcMessageBroadcastReceiver----------->"+line);
			Message msg = Message.obtain(handler, line);
			Bundle data = new Bundle();
			data.putLong("currentTime", currentTime);
			msg.setData(data);
			handler.sendMessage(msg);
		}
		
	}
}
